package com.sxz.sbs.module.performance.fragment;


import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.sxz.sbs.R;
import com.sxz.sbs.app.AppConstant;
import com.sxz.sbs.app.GlideApp;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseFragment;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.performance.PerformanceRankActivity;
import com.sxz.sbs.module.performance.adapter.MonthRankAdapter;
import com.sxz.sbs.module.performance.mode.MonthRankBean;
import com.sxz.sbs.module.performance.presenter.MonthRankPresenter;
import com.sxz.sbs.module.performance.view.MonthRankView;
import com.sxz.sbs.util.DateUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * 月榜
 */
public class MonthRankFragment extends BaseFragment<MonthRankPresenter> implements MonthRankView {
    @BindView(R.id.tv_month_rank_id)
    TextView tvMonthRankId;
    @BindView(R.id.tv_month_rank_abbreviation)
    ImageView tvMonthRankAbbreviation;
    @BindView(R.id.tv_month_rank_name)
    TextView tvMonthRankName;
    @BindView(R.id.tv_month_rank_house)
    TextView tvMonthRankHouse;
    @BindView(R.id.tv_month_rank_field_number)
    TextView tvMonthRankFieldNumber;
    @BindView(R.id.tv_month_rank_praise_number)
    TextView tvMonthRankPraiseNumber;
    @BindView(R.id.iv_month_rank_praise)
    ImageView ivMonthRankPraise;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.swipeRefresh)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.line_view)
    View line;
    @BindView(R.id.fragment_month_rank)
    FrameLayout fragmentRank;
    private MonthRankAdapter monthRankAdapter;
    private List<MonthRankBean.ListBeanX.ListBean> data = new ArrayList<>();
    private int currentPage = 1;
    boolean isList = true;
    private MonthRankBean.ModelBean mbmb;
    @Override
    protected void init() {
        tvMonthRankId.setVisibility(View.INVISIBLE);
        line.setVisibility(View.GONE);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getMonthRank();
            }
        });
        refreshLayout.setRefreshing(true);
        getMonthRank();
        monthRankAdapter = new MonthRankAdapter(data);
        recyclerView.setAdapter(monthRankAdapter);
        recyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (!DateUtils.isSameMonth(((PerformanceRankActivity)getActivity()).getCurrentDate())) {
                    ToastUtils.showShort("此月已经不可点赞哟");
                    return;
                }
                addPraise(data.get(position).getManager_Id());
            }
        });
        monthRankAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage ++;
                getMonthRank();
            }
        },recyclerView);
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                refreshLayout.setRefreshing(true);
                getMonthRank();
            }
        });
        ivMonthRankPraise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!DateUtils.isSameMonth(((PerformanceRankActivity)getActivity()).getCurrentDate())) {
                    ToastUtils.showShort("此月已经不可点赞哟");
                    return;
                }
                if (mbmb != null) {
                    addPraise(mbmb.getManager_Id());
                }

            }
        });
    }

    private void getMonthRank () {
        isList = true;
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-01 00:00:00");
        String begin = simpleDateFormat.format(((PerformanceRankActivity)getActivity()).getCurrentDate());
        String end = simpleDateFormat.format(DateUtils.nextMonthFirstDate(((PerformanceRankActivity)getActivity()).getCurrentDate()));
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("beginTime",begin);
        map.put("endTime",end);
        map.put("pageNumber",currentPage);
        map.put("pageSize", AppConstant.PAGE_SIZE);
        mPresenter.getMonthRank(map);
    }
    private void addPraise (int id) {
        isList = false;
        HashMap<String,Object> map = HttpParamMap.getParamMap();
        map.put("Praise_ManagerId",id);
        mPresenter.addMonthPraise(map);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.month_rank_fragment_layout;
    }

    @Override
    protected MonthRankPresenter createPresenter() {
        return new MonthRankPresenter();
    }
    public void update () {
        refreshLayout.setRefreshing(true);
        currentPage = 1;
        getMonthRank();
    }
    @Override
    public void onGetMothRankSuccess(MonthRankBean bean) {
        if (mIsLive && bean != null) {
            refreshLayout.setRefreshing(false);
            mbmb = bean.getModel();
            GlideApp.with(this).load(mbmb.getUrl_headphoto()).placeholder(R.drawable.ic_headportrait)
                    .error(R.drawable.ic_headportrait).circleCrop().into(tvMonthRankAbbreviation);
            if (mbmb.getIsParise() == 0) {
                ivMonthRankPraise.setImageResource(R.drawable.ic_thumb);
            } else {
                ivMonthRankPraise.setImageResource(R.drawable.ic_thumb_blue);
            }
            tvMonthRankName.setText(bean.getModel().getTxt_name());
            tvMonthRankHouse.setText(mbmb.getCity()==null?"":mbmb.getCity()+"  "
                    +mbmb.getParkingName()==null?"暂无案场":mbmb.getParkingName());
            tvMonthRankFieldNumber.setText(mbmb.getCountNum()+"个");
            tvMonthRankPraiseNumber.setText(mbmb.getPraiseNum()+"");
            if (bean.getList() != null && bean.getList().getList().size() >0 ) {
                stateLayout.showContent();
                if (currentPage == 1) {
                    data.clear();
                    data.addAll(bean.getList().getList());
                    monthRankAdapter.notifyDataSetChanged();
                } else {
                    data.addAll(bean.getList().getList());
                    monthRankAdapter.notifyDataSetChanged();
                }

                monthRankAdapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    data.clear();
                    monthRankAdapter.notifyDataSetChanged();
                } else {
                    if (data.size() > AppConstant.PAGE_SIZE) {
                        monthRankAdapter.loadMoreEnd();
                    } else {
                        monthRankAdapter.loadMoreEnd(true);
                    }
                }
            }
        }
    }

    @Override
    public void onAddMonthPraiseSuccess() {
        currentPage = 1;
        getMonthRank();
    }

    @Override
    public void showError(int errorTag, String msg) {
        if (isList) {
            refreshLayout.setRefreshing(false);
            if (currentPage == 1) {
                if (errorTag == AppConstant.NET_ERROR_CODE) {
                    stateLayout.showNetwork();
                }
            } else {
                monthRankAdapter.loadMoreFail();
            }
        }else {
            super.showError(errorTag,msg);
        }
    }

}
